package com.tutu.utils;

import com.tutu.entity.User;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Program: Spring_New
 * @Description: 模拟ClassPathXmlApplicationContext读取配置文件获取Bean实例
 * @Author: JK.TuTu
 * @Create: 2022-09-13 21:46
 **/
public class MyClassPathXmlApplicationContext {

    Map<String, Object> container = new HashMap<String, Object>();

    public MyClassPathXmlApplicationContext(String resources)  {

        //获得resources对应的文件路径或输入流
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(new File(resources));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        //使用xml解析技术(dom4j)读取xml文件的信息
        SAXReader reader = new SAXReader();
        try {
            Document document = reader.read(inputStream);

            //读出bean标签，属性，以及对应的属性值
            Element rootElement = document.getRootElement();//读取根节点 beans
            List<Element> elements = rootElement.elements();
            for (Element element : elements) {
                List<Attribute> attributes = element.attributes();//读出标签对应的属性
                String id = "";
                String clazz = "";
                for (Attribute attribute : attributes) {
                    if (attribute.getName().equals("id")) {
                        id = attribute.getValue();
                        System.out.println("id=" + id);
                    } else if (attribute.getName().equals("class")) {
                        clazz = attribute.getValue();
                        System.out.println("class=" + clazz);

                    }

                }
                //使用反射生成实例
                Class<?> aClass = null;
                try {
                    aClass = Class.forName(clazz);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                Object obj = null;
                try {
                    obj = aClass.newInstance();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                //使用map存储id和对应的实例
                container.put(id, obj);
            }

        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public Object getBean(String id) {
        return container.get(id);
    }

    public static void main(String[] args) throws FileNotFoundException{
        MyClassPathXmlApplicationContext myClassPathXmlApplicationContext = null;
        try {
            myClassPathXmlApplicationContext = new MyClassPathXmlApplicationContext("E:\\Code\\Spring_New\\Spring_01_IoC\\src\\main\\resources\\applicationContext.xml");
        } catch (Exception e) {
            e.printStackTrace();
        }
        User user = (User) myClassPathXmlApplicationContext.getBean("user");
        user.setName("tutu");
        user.setPassword("123322");
        System.out.println(user);

    }
}

